package com.wlcorp.forcazamis.dao.storagein.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.storagein.IStorageInSampleDAO;
import com.wlcorp.forcazamis.domain.storagein.StorageInSample;

public class StorageInSampleDAOImpl extends HibernateDaoSupport implements
		IStorageInSampleDAO {

	@SuppressWarnings("unchecked")
	public List getStorageInSampleList(final StorageInSample storageInSample)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer("select storageInSample from StorageInSample as storageInSample where 1=1 and storageInSample.deleteFlag=false");
		if(null != storageInSample){
			if(null != storageInSample.getStorageInId()){
				queryString.append(" and storageInSample.storageIn.storageInId = ").append(storageInSample.getStorageInId());
			}
		}
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != storageInSample && null != storageInSample.getMinRownum()){
					query.setFirstResult(storageInSample.getMinRownum());
					query.setMaxResults(storageInSample.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}

	public Long getStorageInSampleCount(StorageInSample storageInSample)
			throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(storageInSample) from StorageInSample as storageInSample where 1=1 and storageInSample.deleteFlag=false");
		if(null != storageInSample){
			if(null != storageInSample.getStorageInId()){
				queryString.append(" and storageInSample.storageIn.storageInId = ").append(storageInSample.getStorageInId());
			}
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
		});
	}

	public StorageInSample getStorageInSampleById(Long storageInSampleId)
			throws DaoException {
		return (StorageInSample)getHibernateTemplate().get(StorageInSample.class,storageInSampleId);
	}

	public void deleteStorageInSample(StorageInSample storageInSample)
			throws DaoException {
		storageInSample.setDeleteFlag(true);
		getHibernateTemplate().update(storageInSample);
	}

	public void saveStorageInSample(StorageInSample storageInSample)
			throws DaoException {
		getHibernateTemplate().saveOrUpdate(storageInSample);
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
	}

}
